<template>
  <el-form class="login-student" ref="form" :model="formData" :rules="rules">
    <el-form-item prop="idCode">
      <el-input
        v-model="formData.idCode"
        :placeholder="$tInput('student.idCode')"
        @keyup.enter.native="submitForm"
        prefix-icon="el-icon-office-building"
      >
        <!-- <SvgIcon slot="prefix" name="xuexiao-copy" /> -->
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="formData.password"
        :placeholder="$tInput('field.password')"
        type="password"
        @keyup.enter.native="submitForm"
        prefix-icon="el-icon-lock"
      >
        <!-- <SvgIcon slot="prefix" name="mima" /> -->
      </el-input>
    </el-form-item>
    <el-button
      class="login-form-btn"
      type="primary"
      @click="submitForm"
      :loading="submiting"
      >登录</el-button
    >
    <div class="login-form-links">
      <el-link
        type="primary"
        icon="el-icon-edit"
        @click="$router.push('/find-pass')"
        >忘记密码</el-link
      >
      <el-link type="primary" @click="$router.push('/register-student')"
        >学生注册</el-link
      >
    </div>
  </el-form>
</template>

<script>
import { IS_DEV } from 'app/config'
import { login } from 'services/student/auth'
import { setToken } from 'app/global'
export default {
  name: 'LoginStudent',
  data() {
    return {
      formData: {
        idCode: IS_DEV ? '47644689800909707' : '',
        password: IS_DEV ? '123456' : ''
      },
      submiting: false
    }
  },
  computed: {
    rules() {
      return {
        idCode: this.$rule('student.idCode', {
          required: true
        }),
        password: this.$rule('field.password', {
          required: true
        })
      }
    }
  },
  methods: {
    async submitForm() {
      try {
        const valid = await this.$refs.form.validate()
        if (!valid) {
          return
        }
        this.submiting = true
        const { data } = await login(this.formData)
        setToken(data.token)
        this.$router.push('/')
      } catch {
      } finally {
        this.submiting = false
      }
    }
  }
}
</script>
